<?php
declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Logic\Tags as TagsLogic;
use App\Http\Requests\Tags as TagsRequest;
use Illuminate\Support\Facades\Validator;

class TagsController extends Controller
{
    private $tagsLogic;
    private $tagsRequest;

    public function __construct(TagsLogic $tagsLogic){
        $this->tagsLogic = $tagsLogic;
        $this->tagsRequest = new TagsRequest();
    }

    public function index(){
        return view('tags.index');
    }

    public function list(Request $request)
    {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        $start = ($page - 1) * $limit;
        $where['tag_name'] = $request->input('name', '');
        $result = [
            'code' => 0,
            'msg' => '获取数据成功',
            'count' => $this->tagsLogic->count($where),
            'data' =>  $this->tagsLogic->list((int)$start, (int)$limit, $where),
        ];
        return response()->json($result);
    }

    public function add(Request $request)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'新增失败！'];
            $data['tag_name'] = $request->post('name','');
            $data['tag_status']= $request->post('status','');
            $data['tag_sort'] = $request->post('sort',0);

            $validator = Validator::make($data, $this->tagsRequest->rules(), $this->tagsRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            if(!$this->tagsLogic->checkName($data['tag_name'])){
                $result['msg'] = '标签名已经存在！';
                return response()->json($result);
            }

            if ($this->tagsLogic->add($data)){
                $result['status'] = true;
                $result['msg'] = '新增成功！';
                return response()->json($result);
            }

            return response()->json($result);

        } else {
            return view('tags.add');
        }
    }

    public function edit(Request $request, int $id = 0)
    {
        if ($request->isMethod('POST')) {
            $result = ['status'=>false, 'msg'=>'修改失败！'];

            $id= $request->post('id',0);
            $data['tag_name'] = $request->post('name','');
            $data['tag_status']= $request->post('status','');
            $data['tag_sort'] = $request->post('sort',0);

            if(($id < 1) || !($this->tagsLogic->has((int)$id))){
                $result['msg'] = '请勿非法提交！';
                return response()->json($result);
            }

            $validator = Validator::make($data, $this->tagsRequest->rules(), $this->tagsRequest->messages());
            if ($validator->fails()) {
                $result['msg'] = $validator->errors()->first();
                return response()->json($result);
            }

            if(!$this->tagsLogic->checkName($data['tag_name'], (int)$id)){
                $result['msg'] = '标签名已经存在！';
                return response()->json($result);
            }

            if ($this->tagsLogic->update((int)$id, $data)){
                $result['status'] = true;
                $result['msg'] = '修改成功！';
                return response()->json($result);
            }

            return response()->json($result);

        } else {
            if ($id < 1) abort(404);
            $data = $this->tagsLogic->find((int)$id);
            if (empty($data)) abort(404);
            return view('tags.edit', ['data' => $data]);
        }
    }

    public function status(int $id = 0)
    {
        $data = ['status' => false, 'msg' => '状态设置失败！'];
        if ($id >= 1){
            if ($this->tagsLogic->status((int)$id)){
                $data['status'] = true;
                $data['msg'] = '状态设置成功！';
                return response()->json($data);
            }
        }
        return response()->json($data);
    }

    public function delete(int $id = 0)
    {
        $result = ['status'=>false, 'msg'=>'删除失败！'];
        if(($id < 1) || !($this->tagsLogic->has((int)$id))){
            $result['msg'] = '请勿非法提交！';
            return response()->json($result);
        }

        $articleCount = $this->tagsLogic->countArticlesByTagId((int)$id);
        if ($articleCount > 0){
            $result['msg'] = '该标签关联了文章，不能删除！';
            return response()->json($result);
        }

        if ($this->tagsLogic->delete((int)$id)){
            $result['status'] = true;
            $result['msg'] = '删除成功！';
            return response()->json($result);
        }
        return response()->json($result);
    }
}
